using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.Tiles.Contents;
using System.Web;

namespace Sedna.Core.Controls
{
	public class TopUserInfo : WebControl, IContent, IConfigurationAware, INamingContainer
	{

		private ITopUserInfoConfiguration configuration;
		private string configurationId = "configuration.TopUserInfo";

		private String contentPlaceHolderId;

		public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public IConfiguration Configuration
		{
			get { return configuration; }
			set { configuration = (ITopUserInfoConfiguration)value; }
		}

		[Bindable(true), Category("Sedna"), DefaultValue("configuration.TopUserInfo")]
		public string ConfigurationId
		{
			get
			{
				EnsureChildControls();
				return configurationId;
			}
			set
			{
				EnsureChildControls();
				configurationId = value;
			}
		}

		public TopUserInfo()
		{
			
		}

		protected override void OnInit(EventArgs e)
		{
			configuration = (ITopUserInfoConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);
			//Build(this);
			base.OnInit (e);
		}

		protected override void CreateChildControls()
		{
			Build(this);
		}


		private void Build(Control parent)
		{
			if(configuration == null)
			{
				configuration = (ITopUserInfoConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);
			}
			TableCell td = new TableCell();
			parent.Controls.Add(td);
			td.CssClass = "userinfo";
			td.Style.Add("border-bottom", "1px solid gray");
			td.Attributes.Add("nowrap", "true");

            string nameLabel = configuration.PrincipalWrapper.FullName;
            if (nameLabel != null) nameLabel = nameLabel.Trim();

            if (string.IsNullOrEmpty(nameLabel))
                nameLabel = configuration.PrincipalWrapper.IdentityName;

            if (nameLabel.Length>20)
		        nameLabel = nameLabel.Substring(0, 20);
		    
			td.Controls.Add(new System.Web.UI.LiteralControl(configuration.GetLocalizedPostfixLabel("textLabel")));
            td.Controls.Add(new System.Web.UI.LiteralControl(nameLabel));

			td.Controls.Add(new System.Web.UI.LiteralControl("["));

			System.Web.UI.WebControls.LinkButton logoutButton = new System.Web.UI.WebControls.LinkButton();
			td.Controls.Add(logoutButton);
			logoutButton.Text = configuration.GetLocalizedPostfixLabel("logoutLabel");
			logoutButton.Click += new EventHandler(OnLogoutClick);
			logoutButton.CausesValidation = false;

			td.Controls.Add(new System.Web.UI.LiteralControl("] ["));

			System.Web.UI.WebControls.LinkButton profileButton = new System.Web.UI.WebControls.LinkButton();
			td.Controls.Add(profileButton);
			profileButton.Text = configuration.GetLocalizedPostfixLabel("profileLabel");
			profileButton.Click += new EventHandler(OnProfileClick);
			profileButton.CausesValidation = false;

			td.Controls.Add(new System.Web.UI.LiteralControl("]"));
		}

		private void OnLogoutClick(object source, EventArgs e)
		{
			//configuration.Authenticator.SignOut(Context);
			//PrincipalCookieUtil.SignOut(Context);
            //HttpCookie cookie = new HttpCookie("SESSID", "0");
            //cookie.Expires = DateTime.Now.AddHours(1);
            //HttpContext.Current.Response.Cookies.Add(cookie);
            //HttpContext.Current.Request.Cookies.Add(cookie);
            //Context.Response.Redirect(configuration.DefaultPage, true);
            ControlUtil.ExecuteAction(ExecutionContext.GetCurrent(ClientID), Context, this.Configuration, this.ClientID, "onLogout");

		}

		private void OnProfileClick(object source, EventArgs e)
		{
			ControlUtil.ExecuteAction(ExecutionContext.GetCurrent(ClientID), Context, this.Configuration, this.ClientID, "onProfile");
		}
	}
}
